package com.njupt.StackAndQueue;

import java.util.Deque;
import java.util.LinkedList;
import java.util.concurrent.DelayQueue;

/**
 * @Author: wujiaming
 * @CreateTime: 2024/11/16 20:37
 * @Description: 1047 删除字符串中所有相邻重复项
 * @Version: 1.0
 */


public class RemoveDuplicates {


    public String removeDuplicates(String s) {
        StringBuilder sb = new StringBuilder(s);
        Deque<Character> stack = new LinkedList<Character>();

        for (int i = 0; i < sb.length(); i++) {

            if(!stack.isEmpty()){
                if(stack.peek() == sb.charAt(i)){
                    sb.delete(i-1,i+1);
                    stack.pop();
                    i -= 2;
                    continue;
                }
            }
            stack.push(sb.charAt(i));
        }
        return sb.toString();
    }


    public static void main(String[] args) {
        RemoveDuplicates test = new RemoveDuplicates();
        String s = "a";
        String s1 = test.removeDuplicates(s);
        System.out.println(s1);
    }
}
